using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Serialization;
using Sirenix.Utilities;
using System;



#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
using Sirenix.OdinInspector.Editor;
using Pangoo.Common;


namespace MetaTable
{

    [CreateAssetMenu(fileName = "MetaTableMainConfig", menuName = "MetaTable/MainConfig", order = 0)]
    public partial class MetaTableMainConfig : ScriptableObject
    {

#if UNITY_EDITOR
        static MetaTableMainConfig m_Instance;
        public static MetaTableMainConfig Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = AssetDatabaseUtility.FindAssetFirst<MetaTableMainConfig>();
                }

                return m_Instance;
            }
        }
#endif

        List<MetaTableConfig> m_MetaTableConfigs = new List<MetaTableConfig>();

        public List<MetaTableConfig> MetaTableConfigs
        {
            get
            {
                return m_MetaTableConfigs;
            }
        }


        public class ConfigEntry
        {
            [ReadOnly]
            public MetaTableConfig Config;

            [ShowInInspector]
            public string Namespace => Config?.Namespace ?? string.Empty;
            [ShowInInspector]
            public string CnName
            {
                get
                {
                    return Config?.Name ?? string.Empty;
                }
                set
                {
                    if (Config != null)
                    {
                        Config.Name = value;
                    }
                }
            }


            [ShowInInspector]
            public string PackageDir => Config?.PackageDir ?? string.Empty;


            [ShowInInspector]
            public bool CheckPackageDir => Config?.CheckPackageDir() ?? false;
        }

        List<ConfigEntry> m_ConfigEntries;

        [ShowInInspector]
        [TableList(AlwaysExpanded = true)]
        [LabelText("配置列表")]
        public List<ConfigEntry> ConfigEntries
        {
            get
            {
                if (m_ConfigEntries == null)
                {
                    m_ConfigEntries = new List<ConfigEntry>();
                    foreach (var config in m_MetaTableConfigs)
                    {

                        m_ConfigEntries.Add(new ConfigEntry
                        {
                            Config = config
                        });
                    }

                }
                return m_ConfigEntries;
            }
            set
            {
                m_ConfigEntries = value;
            }
        }


        [Button("新配置")]
        public void NewConfig()
        {
            var newConfig = new MetaTableNewConfig();
            var window = OdinEditorWindow.InspectObject(newConfig);
            newConfig.Window = window;
            newConfig.MainConfig = this;

        }

        [Button("更新所有MetaTableConfig")]
        public void UpdateAllConfigs()
        {
            m_MetaTableConfigs = AssetDatabaseUtility.FindAsset<MetaTableConfig>().ToList();
            m_ConfigEntries = null;
        }

    }
}

#endif